#include <QCoreApplication>
#include <QtSql>
#include <QDebug>
#include <QSqlQuery>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    // Set up code that uses the Qt event loop here.
    // Call a.quit() or a.exit() to quit the application.
    // A not very useful example would be including
    // #include <QTimer>
    // near the top of the file and calling
    // QTimer::singleShot(5000, &a, &QCoreApplication::quit);
    // which quits the application after 5 seconds.

    // If you do not need a running Qt event loop, remove the call
    // to a.exec() or use the Non-Qt Plain C++ Application template.

    auto db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("127.0.0.1");
    db.setUserName("root");
    db.setPassword("Stallman7*");
    db.setDatabaseName("world");

    auto ret = db.open();

    if (!ret){
        qDebug() << db.lastError().text();
        return 0;
    }

    QSqlQuery query(db);
    ret = query.exec("select count(*) as total from city;");

    if (!ret){
        qDebug() << query.lastError().text();
        return 0;
    }

    //获取记录数量
    bool ok;
    query.first();
    qDebug() << query.value(0).toInt(&ok);
    qDebug() << ok;




    return a.exec();
}
